package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.ware.feign.ProductFeignService;
import com.atguigu.gulimall.ware.vo.SkuHasStockVo;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.WareSkuDao;
import com.atguigu.gulimall.ware.entity.WareSkuEntity;
import com.atguigu.gulimall.ware.service.WareSkuService;
import org.springframework.transaction.annotation.Transactional;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {
   @Autowired
   private WareSkuDao wareSkuDao;
   @Autowired
   private ProductFeignService productFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        /**
         * page: 1
         * limit: 10
         * skuId:
         * wareId:
         */
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String skuId= (String) params.get("skuId");
        String wareId=(String)params.get("wareId");
        if (StringUtils.isNotEmpty(skuId)){
            wrapper.eq("sku_id",skuId);
        }
        if (StringUtils.isNotEmpty(wareId)){
            wrapper.eq("ware_id",wareId);
        }
        IPage<WareSkuEntity> page = this.page(new Query<WareSkuEntity>().getPage(params),wrapper);

        return new PageUtils(page);
    }
    @Transactional
    @Override
    public void addStock(Long wareId, Long skuId, Integer skuNum) {
        List<WareSkuEntity> wareSkuEntities = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (wareSkuEntities==null||wareSkuEntities.isEmpty()){
            WareSkuEntity wareSkuEntity=new WareSkuEntity();
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStockLocked(0);
            //TODO: 另外一种方法，不因为远程服务获取skuName失败而导致事务回滚。一种是try catch,另一种高级再来说。

            try {
                R info = productFeignService.info(skuId);
                Map<String,Object>data= (Map<String, Object>) info.get("skuInfo");
                if(info.getCode()==0){
//                   String skuName= (String) data.get("skuName");
                   wareSkuEntity.setSkuName((String) data.get("skuName"));

                }
            } catch (Exception e) {
//                throw new RuntimeException(e);
            }
            wareSkuDao.insert(wareSkuEntity);
        }
        else {
            wareSkuDao.addStock(wareId,skuId,skuNum);
        }
//       用现有方法不能实现要求，需要自己来写方法，所以自动注入Dao.
//        WareSkuEntity wareSkuEntity = new WareSkuEntity();
//        wareSkuEntity.setWareId(wareId);
//        wareSkuEntity.setSkuId(skuId);
//        wareSkuEntity.setStock(skuNum);

    }

    @Override
    public List<SkuHasStockVo> getSkuHasStock(List<Long> skuIds) {
        List<SkuHasStockVo> collect = skuIds.stream().map((skuId) -> {
            SkuHasStockVo vo = new SkuHasStockVo();
            //SELECT SUM(stock-stock_locked) FROM `wms_ware_sku` WHERE sku_id =?
            Long count= baseMapper.getSkuStock(skuId);
            vo.setSkuId(skuId);
            vo.setHasStock(count != null && count > 0);

            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

}